/**
 * 
 */
package controller;

import java.lang.reflect.Array;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.TwoLineListItem;

import com.actionbarsherlock.app.SherlockFragment;
import com.example.remotecontrol.R;

import common.DimensionHelper;
import components.FragmentMediator;
import components.SMSCommandManager;

/**
 * @author User
 * 
 */
public class SettingFragment extends SherlockFragment implements OnItemSelectedListener{

	private ListView mListView;
	private ArrayList<String[]> mListItem = new ArrayList<String[]>();

	/**
	 * 
	 */
	public SettingFragment() {
		// TODO Auto-generated constructor stub
		mListItem.add(new String[] { "1", "1.1" });
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.setting_fragment, container,
				false);
		
		mListView = (ListView) view.findViewById(R.id.list_view);
		ArrayAdapter adapter = new ArrayAdapter<String[]>(this.getActivity(),
				android.R.layout.simple_list_item_2, mListItem) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				TwoLineListItem row;
				if(convertView == null){
	                LayoutInflater inflater = (LayoutInflater)SettingFragment.this.getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	                row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);                    
	                row.getText1().setTextColor(Color.BLACK);
	                row.getText2().setTextColor(Color.BLACK);
				}else{
	                row = (TwoLineListItem)convertView;
	            }
				String[] item = mListItem.get(position);

				if (item != null && Array.getLength(item) == 2) {
					row.getText1().setText(item[0]);
					row.getText2().setText(item[1]);
				}
				
				return row;
			}
			
			
		};

		mListView.setAdapter(adapter);
//		mListView.setOnItemSelectedListener(new OnItemSelectedListener() {
//			@Override
//			public void onItemSelected(AdapterView<?> arg0, View arg1,
//					int arg2, long arg3) {
//				// TODO Auto-generated method stub
//
//				if (arg3 == 0) {
//						//TODO: Push to Fragment Set text
//					Toast.makeText(getActivity(), "Selected", Toast.LENGTH_LONG).show();
//				}
//			}
//			
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
//		
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if (arg3 == 0) {
					//TODO: Push to Fragment Set text
					Fragment phone = FragmentMediator.CreateFragment(FragmentMediator.FRAGMENT_PHONE);
					FragmentMediator.transitView(SettingFragment.this, phone, null, true);
			} else if (arg3 == 1) {
				Fragment phone = FragmentMediator.CreateFragment(FragmentMediator.FRAGMENT_PASSCODE);
				FragmentMediator.transitView(SettingFragment.this, phone, null, true);
			}
			}
		});
		return view;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
		getView().findViewById(R.id.root_layout).setPadding(
				0, DimensionHelper.getPxFromDp(getActivity(), 100), 0, 0);
		if (mListItem == null) {
			mListItem = new ArrayList<String[]>();
		}
		
		mListItem.clear();
		String phoneNumber = SMSCommandManager.sharedPhoneNumber(getActivity());
		if (phoneNumber == null) {
			phoneNumber = "";
		}
		mListItem.add(new String[] { this.getString(R.string.phone_number),  phoneNumber});
		mListItem.add(new String[] { this.getString(R.string.passcode), ""});
	}
}
